package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.config.result.ResultCode;
import com.future.hair.server.custom.Vo.OrderVo;
import com.future.hair.server.custom.Vo.WorksVo;
import com.future.hair.server.entity.*;
import com.future.hair.server.mapper.*;
import com.future.hair.server.model.PageModel;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("//hair/worksVo")
@Api(tags = "worksVo管理接口")
public class WorksVoController {

    @Autowired
    private WorksMapper mapper;

    @Autowired
    private MallMapper mallMapper;

    @Autowired
    private UserInfoMapper infoMapper;

    @Autowired
    private WorksPraiseMapper praiseMapper;

    @Autowired
    private WorksCommentMapper commentMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "查询 Works 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<WorksVo>> worksVoList(@RequestBody PageModel data, @RequestHeader String token) {
        List<WorksVo> worksVoList = new ArrayList<WorksVo>();
        List<Works> worksList = worksList(data);
        for (Works info:
                worksList) {
            worksVoList.add(getWorksVo(info,token));
        }
        return CommonResult.data(worksVoList,mapper.selectCount(null).toString());
    }

    @ApiOperation(value = "查询当前 WorksVo")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<WorksVo> worksVoId(@RequestBody Works data, @RequestHeader String token) {
        return CommonResult.data(getWorksVo(mapper.selectById(data.getId()),token));
    }

    public List<Works> worksList(@RequestBody PageModel data) {
        QueryWrapper<Works> worksWrapper = new QueryWrapper<Works>();
        if (null!=data.getCategoryId()){
            worksWrapper.eq("category_id",data.getCategoryId());
        }
        if (null!=data.getId()){
            worksWrapper.eq("user_info_id",data.getId());
        }
        return mapper.selectPage(new Page<>(data.getSize(), data.getCurrent()), worksWrapper).getRecords();
    }

    public WorksVo getWorksVo(Works data,String token){
        if (null==data){
            return null;
        }

        WorksVo worksVo = new WorksVo();

        worksVo.setId(data.getId());
        worksVo.setCover(data.getCover());
        worksVo.setCategoryId(data.getCategoryId());
        worksVo.setContent(data.getContent());
        worksVo.setImages(data.getImages());
        worksVo.setCreateTime(data.getCreateTime());

        QueryWrapper<WorksPraise> praiseWrapper = new QueryWrapper<WorksPraise>();
        praiseWrapper.eq("works_id", data.getId());
        worksVo.setPraiseCount(praiseMapper.selectCount(praiseWrapper));

        QueryWrapper<WorksComment> commentWrapper = new QueryWrapper<WorksComment>();
        commentWrapper.eq("works_id", data.getId());
        worksVo.setCommentCount(commentMapper.selectCount(commentWrapper));

        UserInfo info = infoMapper.selectById(data.getUserInfoId());
        worksVo.setUserInfo(info);
        worksVo.setMall(mallMapper.selectById(info.getMallId()));

        Category category = categoryMapper.selectById(data.getCategoryId());
        worksVo.setCategoryName(category.getName());
        if (null!=token && !token.trim().equals("")){
            Claims claims = JwtHelper.verifyJwt(token);
            if (null != claims && 4444!=JwtHelper.getByOpenId(token) && 6666 != JwtHelper.getByOpenId(token) && 8888!=JwtHelper.getByOpenId(token)){
                praiseWrapper.eq("works_id", data.getId());
                praiseWrapper.eq("user_id", JwtHelper.getByUserId(token));
                WorksPraise wp = praiseMapper.selectOne(praiseWrapper);
                if (null != wp){
                    if (wp.getWorks()==1){
                        worksVo.setIsPraise(true);
                    }else{
                        worksVo.setIsPraise(false);
                    }
                }else {
                    worksVo.setIsPraise(false);
                }

            }else{
                worksVo.setIsPraise(false);
            }
        }else{
            worksVo.setIsPraise(false);
        }
        return worksVo;
    }
}
